home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / amos / amoslist-0295.lzh / AMOSLIST / text0059.txt < prev    next >
Encoding:
Text File  |  1995-03-01  |  2.2 KB  |  87 lines

  1. On Tue, 31 Jan 1995, Flint wrote:
  2.  
  3. [...]
  4. > How does recursive programming work? 
  5. >
  6.  
  7.     Basically many problems can be solved by having some subtask call
  8.   itself as part of the sol'n.  This is recursion, ie. programs that call
  9.   themselves are recursive programs.  Why use recursion ?  Some recursive
  10.   solutions can be very short and simple compared to other iterative
  11.   solutions.  As an example consider the problem of a 'Flooding' Algorithm.
  12.   You know the 'paint' command in Amos (Note: Amos uses the OS to do its
  13.   flooding).  You can code it many different ways, and most use some
  14.   explicit use of some stack, but the simplest and most elegant (subjective)
  15.   is just to use recursion.  The disadvantage is that this simple method
  16.   is less effecient.  Here is an simple recursive algorithm for flooding
  17.    ar area: (pseudo Amos :) )
  18.  
  19.  
  20.     procedure Flood( x , y , color )
  21.  
  22.         if( Point(x,y) <> color ) 
  23.  
  24.             Ink color : Plot x,y
  25.  
  26.             Flood( x+1 , y   , color )
  27.             Flood( x-1 , y   , color )
  28.             Flood( x   , y+1 , color )
  29.             Flood( x   , y-1 , color )
  30.  
  31.         end if
  32.  
  33.     end proc
  34.  
  35.  
  36.    This works, by calling itslef on the 4 neighbouring pixels around
  37.    the pixel (x,y).  You just have to call it the first time with the
  38.    start pixel (x,y).
  39.  
  40. > Suppose I wanted to make a 
  41. > MineSweeper clone, how would I let the computer show all the fields 
  42. > that aren't surrounded by a bomb whenever I hit a field which is not 
  43. > surrounded by a bomb? 
  44. > Mind, I'm not trying to make a clone here, I just thought it's the best 
  45. > way to illustrate whatever I try to... well, er, try, really :)
  46.  
  47.     This is very much like the 'Flood' algorirthm above ( coincedence 
  48.   :) )...
  49.  
  50.  
  51.     Procedure Show_Mines( mine_x , mine_y )
  52.  
  53.         if( Number_of_Mines( mine_x, mine_y ) = 0 )
  54.             ' surrounded by no mines
  55.             
  56.             ' do whatever code you have to do to
  57.             ' actually show that this mine co-ordinate (x,y)
  58.             ' has actually been turned up/tested/etc...
  59.  
  60.             For y = -1 to 1
  61.                 For x = -1 to 1
  62.                   if( x<>0 and y<>0) 
  63.                       Show_Mines( mine_x + x , mine_y + y ) 
  64.                   endif
  65.                 Next
  66.             Next
  67.  
  68.         end if
  69.     end procedure
  70.  
  71.  
  72.  
  73.     I hope this helps...
  74.  
  75.                         Michael Sikorsky..
  76.  
  77. > Flint.
  78. > "My boy, if ever you are lost at sea, drop right in and think of me."
  79. > - J. Heller
  80.  
  81.